package com.ruoyi.ma.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.ma.domain.MaMaterialSupplier;
import com.ruoyi.ma.mapper.MaMaterialSupplierMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ma.domain.bo.MaMaterialOtherMeasureBo;
import com.ruoyi.ma.domain.vo.MaMaterialOtherMeasureVo;
import com.ruoyi.ma.domain.MaMaterialOtherMeasure;
import com.ruoyi.ma.mapper.MaMaterialOtherMeasureMapper;
import com.ruoyi.ma.service.IMaMaterialOtherMeasureService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 其他计量Service业务层处理
 *
 * @author llb
 * @date 2024-11-01
 */
@RequiredArgsConstructor
@Service
public class MaMaterialOtherMeasureServiceImpl implements IMaMaterialOtherMeasureService {

    private final MaMaterialOtherMeasureMapper baseMapper;
    private final MaMaterialSupplierMapper supplierMapper;

    /**
     * 查询其他计量
     */
    @Override
    public MaMaterialOtherMeasureVo queryById(Long otherMeasureId){
        MaMaterialOtherMeasureVo data = baseMapper.getById(otherMeasureId);
        return data;
    }

    /**
     * 查询其他计量列表
     */
    @Override
    public TableDataInfo<MaMaterialOtherMeasureVo> queryPageList(MaMaterialOtherMeasureBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MaMaterialOtherMeasure> lqw = buildQueryWrapper(bo);
//        Page<MaMaterialOtherMeasureVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<MaMaterialOtherMeasureVo> result = baseMapper.page(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询其他计量列表
     */
    @Override
    public List<MaMaterialOtherMeasureVo> queryList(MaMaterialOtherMeasureBo bo) {
        LambdaQueryWrapper<MaMaterialOtherMeasure> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MaMaterialOtherMeasure> buildQueryWrapper(MaMaterialOtherMeasureBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MaMaterialOtherMeasure> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMeasureQhId() != null, MaMaterialOtherMeasure::getMeasureQhId, bo.getMeasureQhId());
        lqw.eq(bo.getBdId() != null, MaMaterialOtherMeasure::getBdId, bo.getBdId());
        lqw.eq(bo.getZdId() != null, MaMaterialOtherMeasure::getZdId, bo.getZdId());
        lqw.eq(bo.getGysId() != null, MaMaterialOtherMeasure::getGysId, bo.getGysId());
        lqw.eq(bo.getType() != null, MaMaterialOtherMeasure::getType, bo.getType());
        lqw.eq(bo.getSpjr() != null, MaMaterialOtherMeasure::getSpjr, bo.getSpjr());
        lqw.eq(bo.getWyfj() != null, MaMaterialOtherMeasure::getWyfj, bo.getWyfj());
        lqw.eq(bo.getCfklx() != null, MaMaterialOtherMeasure::getCfklx, bo.getCfklx());
        lqw.eq(bo.getOther() != null, MaMaterialOtherMeasure::getOther, bo.getOther());
        lqw.eq(bo.getKlzbj() != null, MaMaterialOtherMeasure::getKlzbj, bo.getKlzbj());
        lqw.eq(bo.getDqythbzj() != null, MaMaterialOtherMeasure::getDqythbzj, bo.getDqythbzj());
        lqw.eq(StringUtils.isNotBlank(bo.getFileId()), MaMaterialOtherMeasure::getFileId, bo.getFileId());
        return lqw;
    }

    /**
     * 新增其他计量
     */
    @Override
    public Boolean insertByBo(MaMaterialOtherMeasureBo bo) {
        // 获取供应商信息
        LambdaQueryWrapper<MaMaterialSupplier> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGysId() != null, MaMaterialSupplier::getSupplierId, bo.getGysId());
        MaMaterialSupplier supplier = supplierMapper.selectById(bo.getGysId());
        bo.setBdId(supplier.getBdId());
        bo.setZdId(supplier.getZdId());
        bo.setGysId(supplier.getSupplierId());
        // 新增
        MaMaterialOtherMeasure add = BeanUtil.toBean(bo, MaMaterialOtherMeasure.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOtherMeasureId(add.getOtherMeasureId());
        }
        return flag;
    }

    /**
     * 修改其他计量
     */
    @Override
    public Boolean updateByBo(MaMaterialOtherMeasureBo bo) {
        MaMaterialOtherMeasure update = BeanUtil.toBean(bo, MaMaterialOtherMeasure.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MaMaterialOtherMeasure entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除其他计量
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取其他计量详情信息
     *
     * @param bo
     * @return
     */
    @Override
    public MaMaterialOtherMeasureVo info(MaMaterialOtherMeasureBo bo) {
        return baseMapper.getInfo(bo);
    }
}
